package com.silverbox.shopper.dubboApi.alipay;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.silverbox.core.annotation.DataSource;
import com.silverbox.core.exception.CoreException;
import com.silverbox.core.exception.CoreException.CoreExceptionCode;
import com.silverbox.core.util.Utils;
import com.silverbox.shopper.dubboApi.alipay.dto.UndiscountWaimaiDTO;
import com.silverbox.shopper.entity.AlipayApplyAuthToken;
import com.silverbox.shopper.entity.AlipayTradeRecord;
import com.silverbox.shopper.entity.OrderThirdTradeRelated;
import com.silverbox.shopper.entity.PayModel;
import com.silverbox.shopper.service.alipay.AlipayService;

@Service
@DataSource(name = DataSource.DATASOURCE_A)
public class AlipayServiceApiImpl implements AlipayServiceApi {

	@Autowired
	private AlipayService alipayService;
	
	@Override
	public List<AlipayApplyAuthToken> getAlipayApplyAuthToken(AlipayApplyAuthToken token) {
		if (token == null) {
			throw new CoreException("token is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		
		if (StringUtils.isEmpty(token.getAlipayUserId())) {
			throw new CoreException("alipayUserId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.getAlipayApplyAuthToken(token);
	}

	@Override
	public Map<String, Object> getUnDiscountAmount(Integer storeId, Long outSupportId, UndiscountWaimaiDTO waimai,
			boolean priceFlag) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (outSupportId == null) {
			throw new CoreException("outSupportId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (null == waimai) {
			throw new CoreException("waimai is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (Utils.isEmpty(waimai.getItems())) {
			throw new CoreException("items is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.getUnDiscountAmount(storeId, outSupportId, waimai, priceFlag);
	}

	@Override
	public int updateAlipayApplyAuthToken(AlipayApplyAuthToken token) {
		if (token == null) {
			throw new CoreException("token is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.updateAlipayApplyAuthToken(token);
	}

	@Override
	public int saveAlipayTradeRecord(AlipayTradeRecord record) {
		if (record == null) {
			throw new CoreException("record is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.saveAlipayTradeRecord(record);
	}

	@Override
	public AlipayTradeRecord findAlipayTradeRecordByOrderNum(String orderNum,String payOrderNum) {
		if (Utils.isEmpty(orderNum)) {
			throw new CoreException("orderNum is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.findAlipayTradeRecordByOrderNum(orderNum,payOrderNum);
	}

	@Override
	public void insertTradeFundChannelByBatch(String orderNum, String tradeNo, Integer storeId, Integer shopperId,
			String platform, Date date, String fundBillList) {
		if(Utils.isEmpty(orderNum)){
			throw new CoreException("orderNum is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(Utils.isEmpty(tradeNo)){
			throw new CoreException("tradeNo is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(null==storeId){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(null==shopperId){
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(Utils.isEmpty(platform)){
			throw new CoreException("platform is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(null==date){
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(Utils.isEmpty(fundBillList)){
			throw new CoreException("fundBillList is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		alipayService.insertTradeFundChannelByBatch(orderNum, tradeNo, storeId, shopperId, platform, date,
				fundBillList);
	}

	@Override
	public PayModel getPayCodeByPayModel(Integer payModel) {
		if(payModel==null){
			throw new CoreException("payModel is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.getPayCodeByPayModel(payModel);
		 
	}

	@Override
	public int saveOrUpdateRecord(OrderThirdTradeRelated orderThirdTrade) {
		if(orderThirdTrade==null){
			throw new CoreException("orderThirdTrade is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return alipayService.saveOrUpdateRecord(orderThirdTrade);
	}

}
